package com.farm.kaiping.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.generator.SnowflakeGenerator;
import cn.hutool.core.util.ObjectUtil;
import com.farm.common.constant.RedisKeyConstants;
import com.farm.kaiping.entity.Building;
import com.farm.kaiping.entity.Pig;
import com.farm.kaiping.entity.Pigpen;
import com.farm.kaiping.po.BuildingPO;
import com.farm.kaiping.po.PigPO;
import com.farm.kaiping.po.PigpenPO;
import com.farm.kaiping.service.BuildingService;
import com.farm.kaiping.service.PigLogicService;
import com.farm.kaiping.service.PigService;
import com.farm.kaiping.service.PigpenService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


@Service
@Slf4j
@AllArgsConstructor
public class PigLogicServiceImpl implements PigLogicService {

	private final BuildingService buildingService;
	private final PigpenService pigpenService;
	private final PigService pigService;
	private final RedisTemplate<String,Object> redisTemplate;

	/**
	 * 批量入圈
	 *
	 * @param pigPO
	 * @return
	 */
	@Override
	public String batchInPen(PigPO pigPO) {

		if (ObjectUtil.isEmpty(pigPO.getBuildingId())) {
			throw new RuntimeException("所在厂房为空");
		}
		if (ObjectUtil.isEmpty(pigPO.getPigpenId())) {
			throw new RuntimeException("所在栏位为空");
		}
		if (ObjectUtil.isEmpty(pigPO.getBatchNo())) {
			throw new RuntimeException("批次号为空");
		}
		if (ObjectUtil.isEmpty(pigPO.getPigCount()) || pigPO.getPigCount() == 0) {
			throw new RuntimeException("数量不可为0");
		}

		List<Pig> pigList = new ArrayList<>();
		for (int i = 0; i < pigPO.getPigCount(); i++) {
			Pig pig = BeanUtil.copyProperties(pigPO, Pig.class);
			pig.setPigNo(new SnowflakeGenerator().next());
			pigList.add(pig);
		}
		boolean result = pigService.saveBatch(pigList);

		if (result) {
			return "入圈成功";
		} else {
			throw new RuntimeException("保存失败");
		}
	}

	@Override
	public List<PigPO> getPigList(PigPO pigPO) {

		if (ObjectUtil.isNotEmpty(pigPO.getBuildingName())){
			BuildingPO buildingPO = new BuildingPO();
			buildingPO.setBuildingName(pigPO.getBuildingName());
			List<Building> buildingList = buildingService.getBuildingList(buildingPO);
			if (buildingList.isEmpty()){
				return new ArrayList<>();
			}
			pigPO.setBuildingIdList(buildingList.stream().map(Building::getId).collect(Collectors.toList()));
		}

		if (ObjectUtil.isNotEmpty(pigPO.getPigpenNo())){
			PigpenPO pigpenPO = new PigpenPO();
			pigpenPO.setPigpenNo(pigPO.getPigpenNo());
			List<Pigpen> pigpenList = pigpenService.getPigpenList(pigpenPO);
			if (pigpenList.isEmpty()){
				return new ArrayList<>();
			}
			pigPO.setPigpenIdList(pigpenList.stream().map(Pigpen::getId).collect(Collectors.toList()));
		}

		List<Pig> selectList = pigService.getPigList(pigPO);

		return selectList.stream().map(pig -> {
			PigPO pigVO = BeanUtil.copyProperties(pig, PigPO.class);
			pigVO.setBuildingName(String.valueOf(redisTemplate.opsForValue().get(RedisKeyConstants.BUILDING_NAME+pig.getBuildingId())));
			pigVO.setPigpenNo(String.valueOf(redisTemplate.opsForValue().get(RedisKeyConstants.PIGPEN_NO+pig.getPigpenId())));
			return pigVO;
		}).collect(Collectors.toList());
	}
}
